/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jellyfishumbrella.GL;

import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTranslatef;

import org.lwjgl.util.vector.Vector3f;

/**
 * 
 * @author 500441547
 */
public class Camera extends AnimObject implements Renderable {
	public static float			FOV				= 50.0f;
	public static boolean		keepCentered	= false;
	public static final float	SPEED			= 300.0f * AbstractGameLogic.GL_SCALE;
	public static final float	CREEP_SPEED		= SPEED * 0.1f;
	public static boolean		CREEP			= false;

	public boolean				isFlying		= false;
	public static boolean		inNewHex		= false;
	public static boolean		inNewField		= false;

	public static float[]		viewMatrix		= { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };

	public Camera() {
		super();
		/*
		keyframes.currKey.pos = new Vector3d(0.0, 250.0, 1250.0);
		keyframes.currKey.rot = new Vector3d(0.0, 0.0, 0.0);
		keyframes.here = ((Keyframe) keyframes.currKey).clone();
		keyframes.endBehavior = KeyframeQueue.END_BEHAVIOR.CONSTANT;
		*/
		this.animMode = ANIM_MODE.DIRECT;
		setPos(new Vector3d(0.0, 500.0, 0.0));
		setRot(new Vector3d(0.0, 90.0, 0.0));
		setScale(new Vector3d(1.0,1.0,1.0));
	}

	public void render() {
		glRotatef(keyframes.here.rot.toGL().x, 1.0f, 0.0f, 0.0f);
		glRotatef(keyframes.here.rot.toGL().y, 0.0f, 1.0f, 0.0f);
		glRotatef(keyframes.here.rot.toGL().z, 0.0f, 0.0f, 1.0f);
		glTranslatef(-keyframes.here.pos.toGL().x, -keyframes.here.pos.toGL().y, -keyframes.here.pos.toGL().z);
	}

	public void update() {}

	public void moveForward(long time) {
		//if (keyframes.nextKey != null) {
			synchronized (keyframes) {
				((Keyframe) keyframes.nextKey).time = time;
				float speed;
				if (CREEP) {
					speed = CREEP_SPEED;
				} else {
					speed = SPEED;
				}
				float xrotrad, yrotrad, yrotradY, yrotradZ;
				xrotrad = (float) Math.PI * ((keyframes.here.rot.toGL().x) / 180.0f);
				yrotrad = (float) Math.PI * ((keyframes.here.rot.toGL().y) / 180.0f);

				/*
				((Keyframe) keyframes.nextKey).pos.x += SPEED * (float) (Math.sin(yrotrad));
				if (isFlying) {
					((Keyframe) keyframes.nextKey).pos.y -= SPEED * (float) (Math.sin(xrotrad));
				}
				((Keyframe) keyframes.nextKey).pos.z -= SPEED * (float) (Math.cos(yrotrad));
				*/
				Vector3d here = pos(0);
				here.x += SPEED * (float) (Math.sin(yrotrad));
				here.z -= SPEED * (float) (Math.cos(yrotrad));
				setPos(here);
			}
		//}
	}

	public void moveBackward(long time) {
		//if (keyframes.nextKey != null) {
			synchronized (keyframes) {
				((Keyframe) keyframes.nextKey).time = time;
				float speed;
				if (CREEP) {
					speed = CREEP_SPEED;
				} else {
					speed = SPEED;
				}
				float xrotrad, yrotrad, yrotradY, yrotradZ;
				xrotrad = (float) Math.PI * ((keyframes.here.rot.toGL().x) / 180.0f);
				yrotrad = (float) Math.PI * ((keyframes.here.rot.toGL().y) / 180.0f);
				
				/*
				((Keyframe) keyframes.nextKey).pos.x -= SPEED * (float) (Math.sin(yrotrad));
				if (isFlying) {
					((Keyframe) keyframes.nextKey).pos.y += SPEED * (float) (Math.sin(xrotrad));
				}
				((Keyframe) keyframes.nextKey).pos.z += SPEED * (float) (Math.cos(yrotrad));
				*/
				Vector3d here = pos(0);
				here.x -= SPEED * (float) (Math.sin(yrotrad));
				here.z += SPEED * (float) (Math.cos(yrotrad));
				setPos(here);
			}
		//}
	}

	public void moveLeft(long time) {
		//if (keyframes.nextKey != null) {
			synchronized (keyframes) {
				((Keyframe) keyframes.nextKey).time = time;
				float speed;
				if (CREEP) {
					speed = CREEP_SPEED;
				} else {
					speed = SPEED;
				}
				float yrotrad, yrotradY, yrotradZ;
				yrotrad = (float) Math.PI * (keyframes.here.rot.toGL().y / 180.0f);

				/*
				((Keyframe) keyframes.nextKey).pos.x -= SPEED * (float) (Math.cos(yrotrad));
				((Keyframe) keyframes.nextKey).pos.z -= SPEED * (float) (Math.sin(yrotrad));
				*/
				Vector3d here = pos(0);
				here.x -= SPEED * (float) (Math.cos(yrotrad));
				here.z -= SPEED * (float) (Math.sin(yrotrad));
				setPos(here);
			}
		//}
	}

	public void moveRight(long time) {
		//if (keyframes.nextKey != null) {
			synchronized (keyframes) {
				((Keyframe) keyframes.nextKey).time = time;
				float speed;
				if (CREEP) {
					speed = CREEP_SPEED;
				} else {
					speed = SPEED;
				}
				float yrotrad, yrotradY, yrotradZ;
				yrotrad = (float) Math.PI * (keyframes.here.rot.toGL().y / 180.0f);

				/*
				((Keyframe) keyframes.nextKey).pos.x += SPEED * (float) (Math.cos(yrotrad));
				((Keyframe) keyframes.nextKey).pos.z += SPEED * (float) (Math.sin(yrotrad));
				*/
				Vector3d here = pos(0);
				here.x += SPEED * (float) (Math.cos(yrotrad));
				here.z += SPEED * (float) (Math.sin(yrotrad));
				setPos(here);
			}
		//}
	}

	public void moveUp(long time) {
		//if (keyframes.nextKey != null) {
			synchronized (keyframes) {
				((Keyframe) keyframes.nextKey).time = time;
				float speed;
				if (CREEP) {
					speed = CREEP_SPEED;
				} else {
					speed = SPEED;
				}
				/*
				((Keyframe) keyframes.nextKey).pos.y += SPEED;// /3.0f;
				// _hexCoords.w += speed;
				*/
				Vector3d here = pos(0);
				here.y += SPEED;
				setPos(here);
			}
		//}
	}

	public void moveDown(long time) {
		//if (keyframes.nextKey != null) {
			synchronized (keyframes) {
				((Keyframe) keyframes.nextKey).time = time;
				float speed;
				if (CREEP) {
					speed = CREEP_SPEED;
				} else {
					speed = SPEED;
				}
				/*
				((Keyframe) keyframes.nextKey).pos.y -= SPEED;// /3.0f;
				// _hexCoords.w -= speed;
				*/
				Vector3d here = pos(0);
				here.y -= SPEED;
				setPos(here);
			}
		//}
	}

	public void rotXoffset(float rotX, long time) {
		//if (keyframes.nextKey != null) {
			synchronized (keyframes) {
				/*
				((Keyframe) keyframes.nextKey).time = time;
				((Keyframe) keyframes.nextKey).rot.x += rotX;
				if (((Keyframe) keyframes.nextKey).rot.x < -90) {
					((Keyframe) keyframes.nextKey).rot.x = -90;
				} else if (((Keyframe) keyframes.nextKey).rot.x > 90) {
					((Keyframe) keyframes.nextKey).rot.x = 90;
				}
				*/
				Vector3d here = rot(0);
				here.x += rotX;
				if(here.x < -90){
					here.x = -90;
				}else if (here.x > 90){
					here.x = 90;
				}
				setRot(here);
			}
		//}
	}

	public void rotYoffset(float rotY, long time) {
		//if (keyframes.nextKey != null) {
			synchronized (keyframes) {
				/*
				((Keyframe) keyframes.nextKey).time = time;
				((Keyframe) keyframes.nextKey).rot.y += rotY;
				*/
				Vector3d here = rot(0);
				here.y += rotY;
				setRot(here);
			}
		//}
	}

	public void rotZoffset(float rotZ, long time) {
		//if (keyframes.nextKey != null) {
			synchronized (keyframes) {
				/*
				((Keyframe) keyframes.nextKey).time = time;
				((Keyframe) keyframes.nextKey).rot.z += rotZ;
				*/
				Vector3d here = rot(0);
				here.z += rotZ;
				setRot(here);
			}
		//}
	}
}
